package com.hht.checkversion;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: chenhu
 * Time: 2019/12/12 17:52
 * Description do somethings
 */
public class VersionAdapter extends BaseAdapter {
    private Context mContext;
    private List<AppBean> mListData;

    public VersionAdapter(Context context, List<AppBean> listData) {
        mContext = context;
        if (listData == null) {
            mListData = new ArrayList<>();
        } else {
            mListData = listData;
        }
    }

    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.top_layout, parent, false);
            mViewHolder.mAppNameTv = convertView.findViewById(R.id.top_app_name_tv);
            mViewHolder.mAppVersionTv = convertView.findViewById(R.id.top_app_version_tv);
            mViewHolder.mAppPackageTv = convertView.findViewById(R.id.top_app_package_tv);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        if (mListData != null && mListData.size() > 0) {
            AppBean appBean = mListData.get(position);
            mViewHolder.mAppNameTv.setText(appBean.getAppName());
            Drawable drawable = appBean.getDrawable();
            if (drawable != null) {
                drawable.setBounds(0, 0, 60, 60);
                mViewHolder.mAppNameTv.setCompoundDrawables(drawable, null, null, null);
            }
            mViewHolder.mAppVersionTv.setText("Version:" + appBean.getVersionInfo());
            mViewHolder.mAppPackageTv.setText(appBean.getPackageName());
        }
        return convertView;
    }

    static class ViewHolder {
        TextView mAppNameTv;
        TextView mAppVersionTv;
        TextView mAppPackageTv;
    }
}
